<template>
  <el-card class="box-card">
    <el-tabs type="border-card">
      <el-tab-pane label="首页">
        <template>
          <el-input
            v-model="page.input"
            placeholder="请输入内容进行查询"
            :clearable="true"
            style="width: 30%; float: left; margin-bottom: 10px"
          ></el-input>

          <el-button
            type="primary"
            @click="loadBorrow"
            style="width: 5%; float: left; margin-left: 10px"
            ><i class="el-icon-search"></i
          ></el-button>
          <el-table :data="tableData" border style="width: 100%">
            <el-table-column prop="userName" label="借贷人" width="120">
            </el-table-column>
            <el-table-column prop="borrowMoney" label="借贷金额" width="100">
            </el-table-column>

            <el-table-column prop="borrowType" label="借贷类型" width="160">
            </el-table-column>
            <el-table-column
              prop="borrowCreattime"
              label="预计还贷完成时间"
              width="140"
            >
            </el-table-column>
            <el-table-column
              prop="borrowPreatendtime"
              label="预计当期还贷时间"
              width="150"
            >
            </el-table-column>
            <el-table-column
              prop="borrowRealityendtime"
              label="实际当期还贷时间"
              width="150"
            >
            </el-table-column>
            <el-table-column prop="borrowStatus" label="借贷状态" width="120">
            </el-table-column>

            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  @click="handleEdit(scope.row.borrowId)"
                  >编辑</el-button
                >
                <el-button
                  type="warning"
                  style="width: 70px"
                  @click="handleWarn(scope.row.borrowId)"
                  >预警</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <!-- --------------------------分页------------------------------ -->
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page.currentPage"
            :page-sizes="pageInfo.pageSizes"
            :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
          >
          </el-pagination>

          <!-- ------------------------修改模态框---------------------------- -->
          <el-dialog title="修改" :visible.sync="dialog.updateshow" width="40%">
            <el-form
              ref="form"
              :model="updateForm"
              label-width="100px"
              size="medium"
            >
              <el-form-item label="借贷人" prop="userName">
                <el-input v-model="updateForm.userName"></el-input>
              </el-form-item>
              <el-form-item label="借款金额" prop="borrowMoney">
                <el-input v-model="updateForm.borrowMoney"></el-input>
              </el-form-item>
              <el-form-item label="借贷类型" prop="borrowType">
                <el-input v-model="updateForm.borrowType" clearable></el-input>
              </el-form-item>

              <el-form-item label="当前还贷时间" prop="borrowRealityendtime">
                <el-col :span="11">
                  <el-date-picker
                    type="date"
                    placeholder="选择日期"
                    v-model="updateForm.borrowRealityendtime"
                    style="width: 100%"
                  ></el-date-picker>
                </el-col>
              </el-form-item>

              <el-form-item label="借贷状态">
                <el-select
                  v-model="updateForm.borrowStatus"
                  placeholder="请选择状态"
                >
                  <el-option label="已结款" value="已结款"></el-option>
                  <el-option label="还款中" value="还款中"></el-option>
                  <el-option label="逾期" value="逾期"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialog.updateshow = false">取 消</el-button>
              <el-button type="primary" @click="update()">确 定</el-button>
            </span>
          </el-dialog>

          <!-- ------------------------预警模态框---------------------------- -->
          <el-dialog
            title="修改"
            :visible.sync="dialog.warningshow"
            width="40%"
          >
            <el-form
              ref="form"
              :model="warningForm"
              label-width="100px"
              size="medium"
            >
              <el-form-item label="预警等级">
                <el-select
                  v-model="warningForm.borwarningGrade"
                  placeholder="请选择等级"
                >
                  <el-option label="蓝色预警" value="蓝色预警"></el-option>
                  <el-option label="黄色预警" value="黄色预警"></el-option>
                  <el-option label="橙色预警" value="橙色预警"></el-option>
                  <el-option label="红色预警" value="红色预警"></el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialog.warningshow = false">取 消</el-button>
              <el-button type="primary" @click="insertWarning()"
                >确 定</el-button
              >
            </span>
          </el-dialog>
        </template>
      </el-tab-pane>

      <!-- ----------------------第二个页面小模块--------------------- -->
      <el-tab-pane label="借贷分析">
        <el-row :gutter="20">
          <el-col :span="6">
            <div>
              <el-statistic
                group-separator=","
                :precision="2"
                :value="value2"
                :title="title"
              ></el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic title="逾期占比">
                <template slot="formatter"> 456/2 </template>
              </el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic
                group-separator=","
                :precision="2"
                decimal-separator="."
                :value="value1"
                :title="title"
              >
                <template slot="prefix">
                  <i class="el-icon-s-flag" style="color: red"></i>
                </template>
                <template slot="suffix">
                  <i class="el-icon-s-flag" style="color: blue"></i>
                </template>
              </el-statistic>
            </div>
          </el-col>
          <el-col :span="6">
            <div>
              <el-statistic :value="like ? 521 : 520" title="Feedback">
                <template slot="suffix">
                  <span @click="like = !like" class="like">
                    <i
                      class="el-icon-star-on"
                      style="color: red"
                      v-show="!!like"
                    ></i>
                    <i class="el-icon-star-off" v-show="!like"></i>
                  </span>
                </template>
              </el-statistic>
            </div>
          </el-col>
        </el-row>

        <div>
          <br />
          <div id="main" style="width: 500px; height: 400px; float: left"></div>
          <div
            id="main2"
            style="width: 650px; height: 400px; float: left"
          ></div>
        </div>
      </el-tab-pane>

      <!-- ----------------------第三个页面小模块--------------------- -->
      <!-- <el-tab-pane label="逾期管理">逾期</el-tab-pane> -->
      <!-- ----------------------第四个页面小模块--------------------- -->
      <!-- <el-tab-pane label="还款中管理">还款中管理</el-tab-pane> -->
    </el-tabs>
  </el-card>
</template>


<script>
export default {
  data() {
    return {
      warning: false,
      sizeForm: {
        region: "",
      },
      warningForm: {
        borwarningGrade: "",
        borwarningDays: 7,
        borrowId: "",
        borwarningStatus: "启用",
      },
      tableData: "",
      updateForm: "",
      like: true,
      value1: 4154.564,
      value2: 2222,
      title: "今年借贷人数增长",

      page: {
        currentPage: 1,
        pagesize: 5,
        input: "",
      },
      pageInfo: {
        total: 0,
        pageSizes: [5, 20, 30, 50, 100],
      },
      dialog: {
        updateshow: false,
        warningshow: false,
      },
      EchartsList: {},
      TypeEcharts: {},
    };
  },
  methods: {
    insertWarning() {
      this.dialog.warningshow = false;
      this.$axios.post("/BorWarning", this.warningForm).then((res) => {
        if (res.data.code == 200) {
          this.$message.success("预警成功");
        }
      });
    },
    handleWarn(id) {
      this.warningForm.borrowId = id;
      this.dialog.warningshow = true;
    },
    update() {
      this.$axios.put("/Borrow", this.updateForm).then((res) => {
        if (res.data.code == 200) {
          this.$message.success(res.data.messages);
          this.dialog.updateshow = false;
        } else {
          this.$message.error(res.data.messages);
        }
      });

      console.log("sss");
      this.loadBorrow();
    },
    //当每页显示条数改变时
    handleSizeChange(size) {
      this.page.pagesize = size;
      this.loadBorrow();
    },
    //当页数改变时
    handleCurrentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.loadBorrow();
    },
    handleEdit(id) {
      this.$axios
        .get("/Borrow/" + id)
        .then((res) => {
          this.updateForm = res.data.data.borrow;
        })
        .catch((error) => {
          this.$message.error("出错了,请稍后重试");
        });
      this.dialog.updateshow = true;
    },
    loadBorrow() {
      this.$axios.post("/Borrow/pageSelect", this.page).then((res) => {
        this.tableData = res.data.data.list;
        let time = this.$moment(new Date()).format("YYYY-MM-DD");
        this.tableData.forEach((element) => {
          console.log(time);
          console.log(element.borrowPreatendtime);
        });
        this.pageInfo.total = res.data.data.pageInfo;
      });
    },
    selectECharts() {
      this.$axios.get("/Borrow/Echarts").then((res) => {
        this.EchartsList = res.data.data.Echarts;
        this.getChar();
      });
      this.$axios.get("/Borrow/TypeEcharts").then((res) => {
        this.TypeEcharts = res.data.data.TypeEcharts;
        this.getChar2();
      });
    },
    getChar() {
      console.log(this.EchartsList + "%%%%%%%%%%5");
      var myChart = echarts.init(document.getElementById("main"));
      var option = {
        title: {
          text: "还贷情况分析",

          left: "center",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "left",
        },
        series: [
          {
            name: "人数",
            type: "pie",
            radius: "50%",
            data: this.EchartsList,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
    getChar2() {
      var myChart = echarts.init(document.getElementById("main2"));
      var option2 = {
        tooltip: {
          trigger: "item",
        },
        legend: {
          top: "5%",
          left: "center",
        },
        series: [
          {
            name: "Access From",
            type: "pie",
            radius: ["40%", "70%"],
            avoidLabelOverlap: false,
            itemStyle: {
              borderRadius: 10,
              borderColor: "#fff",
              borderWidth: 2,
            },
            label: {
              show: false,
              position: "center",
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: "bold",
              },
            },
            labelLine: {
              show: false,
            },
            data: this.TypeEcharts,
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option2);
    },
  },
  mounted() {
    this.loadBorrow();
    this.selectECharts();
  },
};
</script>
